package com.ews.onlineexchange.controller.assess;

import java.util.List;

import javax.enterprise.inject.Model;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.CalcMethod;
import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.assess.AssessClass;
import com.ews.onlineexchange.model.assess.Dict;
import com.ews.onlineexchange.service.EntityQuery;

@Model
public class DictList extends EntityQuery<Dict> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String title;
	private String assessClassId;
	private CalcMethod calcMethod;
	
	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<Dict> entity) {
		List<Predicate> restrictions = super.getRestrictions( cb, entity );
		
		restrictions.add( cb.equal( entity.get( "delFlag" ), DataEntity.DEL_FLAG_NORMAL ) );
		//restrictions.add( cb.isFalse( entity.join( "assessClass" ).get("allowSupplierApply") ) );
		
		if ( StringUtils.isNotBlank( getTitle() ) ) {
			restrictions.add( cb.like( entity.get( "title" ), getStringRestriction( getTitle() ) ) );
		}
		
		if ( getCalcMethod() != null ) {
			restrictions.add( cb.equal( entity.get( "calcMethod" ), getCalcMethod() ) );
		}
		
		if ( StringUtils.isNotBlank( getAssessClassId() ) ) {
			Join<Dict, AssessClass> ac = entity.join( "assessClass" );
			restrictions.add( cb.equal( ac.get( "id" ), getAssessClassId() ) );
		}

		return restrictions;
	}

	public DictList() {
		setMaxResults(null);
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAssessClassId() {
		return assessClassId;
	}

	public void setAssessClassId(String assessClassId) {
		this.assessClassId = assessClassId;
	}

	public CalcMethod getCalcMethod() {
		return calcMethod;
	}

	public void setCalcMethod(CalcMethod calcMethod) {
		this.calcMethod = calcMethod;
	}	

}
